package app.fugema.shop.main.order.view;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.Observer;
import app.fugema.shop.main.order.adapter.OrderListAdapter;
import app.fugema.shop.main.order.model.OrderListResult;

import com.base.common.app.BaseConstant;
import com.base.common.app.LoginUtils;
import com.base.common.netBeanPackage.LiveDataWrapper;
import com.base.common.utils.UIUtils;
import com.base.common.view.base.BaseFragment;
import com.base.common.view.base.OnGetDataListener;
import com.base.common.viewmodel.BaseViewObserver;
import com.jeremyliao.liveeventbus.LiveEventBus;
import app.fugema.shop.R;
import app.fugema.shop.constants.AppConstants;
import app.fugema.shop.databinding.OrderListFmBinding;

import app.fugema.shop.main.order.vm.OrderListVM;

public class OrderListFragment extends BaseFragment<OrderListFmBinding, OrderListVM> implements OnGetDataListener {

    private String userKey;

    public static OrderListFragment getInstance(int type)
    {
        Bundle args = new Bundle();
        args.putInt("type", type);
        OrderListFragment fragment = new OrderListFragment();
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    protected OrderListFmBinding initDataBinding(LayoutInflater inflater, ViewGroup container) {
        return DataBindingUtil.inflate(inflater, R.layout.order_list_fm, container, false);
    }

    private int type; //订单状态  0全部 1:待付款  2:待发货  3:待收货   5:待评价/评价/        (未知) 5:成功 6:失败 false

    private OrderListAdapter orderListAdapter;

    private int ps = 10;


    @Override
    protected void initNotificationReceipt() {
        super.initNotificationReceipt();
        // 待支付订单 支付成功后 刷新
        LiveEventBus.get(BaseConstant.EventKey.PAY_SUCCESS, String.class).observe(this, new Observer<String>() {
            @Override
            public void onChanged(String o)
            {
                if (type == 1)
                {
                    onGetData(1);//付款成功后，订单流转到待发货，支付成功后刷新
                }
                else if ( type == 0)
                {
                    for (int i = 0; i < orderListAdapter.getChildCount(); i++)
                    {
                        // 待修改

                        if (orderListAdapter.getListBean(i).getId() == Long.parseLong(o))
                        {
                            int pn = i / ps + 1;
                            onGetData(pn);
                            break;
                        }

                    }
                }
            }
        });

        //确认收货成功
        LiveEventBus.get(AppConstants.EventKey.ORDER_RECEIPT, String.class).observe(this, new Observer<String>() {
            @Override
            public void onChanged(String o) {
                if (type == 3)
                {
                    //待收货
                    onGetData(1);
                }
                else if (type == 0 || type == 3)
                {
                    for (int i = 0; i < orderListAdapter.getChildCount(); i++)
                    {
                        if (orderListAdapter.getListBean(i).getId() == Long.parseLong(o))
                        {
                            int pn = i / ps + 1;
                            onGetData(pn);
                            break;
                        }
                    }
                }
            }
        });

        //取消订单之后
        LiveEventBus.get(AppConstants.EventKey.ORDER_CANCEL, String.class).observe(this, new Observer<String>() {
            @Override
            public void onChanged(String o)
            {
                if (type == 0)
                {
                    for (int i = 0; i < orderListAdapter.getChildCount(); i++)
                    {
                        // 刷新  该页 的所有 订单
                        if (orderListAdapter.getListBean(i).getId() == Long.parseLong(o) )
                        {
                            int pn = i / ps + 1;
                            onGetData(pn);
                            break;
                        }
                    }
                }
            }
        });
        //订单评价完成 刷新列表
        LiveEventBus.get(AppConstants.EventKey.COMMENT_COMPLETE, String.class).observe(this, new Observer<String>() {
            @Override
            public void onChanged(String o)
            {
                if(type == 5)
                {
                    //待评价
                    onGetData(1);
                }
                if (type == 0 )
                {
                    for (int i = 0; i < orderListAdapter.getChildCount(); i++)
                    {
                        if (orderListAdapter.getListBean(i).getId() == Long.parseLong(o))
                        {
                            int pn = i / ps + 1;
                            onGetData(pn);
                            break;
                        }
                    }
                }
            }
        });
    }

    @Override
    public void initView()
    {
        super.initView();
        orderListAdapter = new OrderListAdapter(this, viewModel);
        binding.rvRecyclerView.setAdapter(orderListAdapter);

        viewUtils.setRefreshStateLayout(binding.rvRecyclerView, this, this);
    }

    @Override
    public void initData()
    {
        super.initData();
        type = getArguments().getInt("type");
        userKey = LoginUtils.getToken();
        onGetData(1);
    }

    @Override
    public void onGetData(int pn)
    {
        MutableLiveData<LiveDataWrapper<OrderListResult>> liveData = viewModel.getMyOrders(type, pn, ps,userKey);
        liveData.observeForever(new BaseViewObserver<LiveDataWrapper<OrderListResult>>(viewUtils.getStateLayout()) {
            @Override
            protected void onSuccess(LiveDataWrapper<OrderListResult> data)
            {

                viewUtils.setDataListRefreshLayout(orderListAdapter, pn, ps, data.data.getOrders());
            }

            @Override
            public boolean isEmptyData(LiveDataWrapper<OrderListResult> data)
            {
                return pn == 1 && UIUtils.isEmpty(data.data.getOrders());
            }

            @Override
            public boolean isRefreshLayoutData()
            {
                return true;
            }

            @Override
            protected void onComplete()
            {
                super.onComplete();
                liveData.removeObserver(this);
            }
        });
    }
}
